package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.GdbMerchantAgreementDTO;
import com.yuanfeng.base.entity.BaseMerchantAgreementEntity;
import com.yuanfeng.base.feign.ThirdPartyServiceClient;
import com.yuanfeng.base.mapper.BaseMerchantAgreementMapper;
import com.yuanfeng.base.service.BaseMerchantAgreementService;
import com.yuanfeng.base.vo.GdbMerchantAgreementVO;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BaseMerchantAgreementServiceImpl
 * @Description 入驻协议
 * @Author cf
 * @Date 2023/6/21 14:04
 * @Version 1.0
 */
@Service
public class BaseMerchantAgreementServiceImpl extends ServiceImpl<BaseMerchantAgreementMapper, BaseMerchantAgreementEntity> implements BaseMerchantAgreementService {

    @Autowired
    private BaseMerchantAgreementMapper baseMerchantAgreementMapper;

    @Autowired
    private ThirdPartyServiceClient thirdPartyServiceClient;

    @Override
    public ResponseResult selectAllAgreement(GdbMerchantAgreementDTO gdbMerchantAgreement) {
        GdbMerchantAgreementVO result = baseMerchantAgreementMapper.selectAgreement(gdbMerchantAgreement);
        return ResponseResult.success(result);
    }

    @Override
    public ResponseResult updateAgreement(MultipartFile file, String sign) {
        //上传协议到oss
        ResponseResult responseResult = thirdPartyServiceClient.uploadToOssFile(file);
        String url = responseResult.getData().toString();
        File pdfFile = new File(url);
        long fileSizeInBytes = pdfFile.length();
        long fileSizeInKB = fileSizeInBytes / 1024; // 将字节数转换为KB
        if(fileSizeInKB > 2048){
            return ResponseResult.fail(BizCodeEnume.FILE_SIZE_GT_TWO);
        }
        //查询协议是否存在
        BaseMerchantAgreementEntity baseMerchantAgreementEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<BaseMerchantAgreementEntity>()
                                                                  .eq(BaseMerchantAgreementEntity::getSign,sign));
        //新增
        if (null == baseMerchantAgreementEntity) {
            baseMerchantAgreementEntity = new BaseMerchantAgreementEntity();
            baseMerchantAgreementEntity.setUrl(url);
            //企业
            if (CommonType.agreement.ME_AGREEMENT.getType().equals(sign)) {
                baseMerchantAgreementEntity.setName(CommonType.agreement.ME_AGREEMENT.getDesc());
                baseMerchantAgreementEntity.setSign(CommonType.agreement.ME_AGREEMENT.getType());
            } else if (CommonType.agreement.US_AGREEMENT.getType().equals(sign)) {
                //个人
                baseMerchantAgreementEntity.setName(CommonType.agreement.US_AGREEMENT.getDesc());
                baseMerchantAgreementEntity.setSign(CommonType.agreement.US_AGREEMENT.getType());

            } else if (CommonType.agreement.GY_ME_AGREEMENT.getType().equals(sign)) {
                //供应商商家
                baseMerchantAgreementEntity.setName(CommonType.agreement.GY_ME_AGREEMENT.getDesc());
                baseMerchantAgreementEntity.setSign(CommonType.agreement.GY_ME_AGREEMENT.getType());
            } else if (CommonType.agreement.GY_US_AGREEMENT.getType().equals(sign)) {
                //供应商用户
                baseMerchantAgreementEntity.setName(CommonType.agreement.GY_US_AGREEMENT.getDesc());
                baseMerchantAgreementEntity.setSign(CommonType.agreement.GY_US_AGREEMENT.getType());
            } else if (CommonType.agreement.GEN_COR_AGREEMENT.getType().equals(sign)) {
                //通用
                baseMerchantAgreementEntity.setName(CommonType.agreement.GEN_COR_AGREEMENT.getDesc());
                baseMerchantAgreementEntity.setSign(CommonType.agreement.GEN_COR_AGREEMENT.getType());
            }else if(CommonType.agreement.BR_AGREEMENT.getType().equals(sign)) {
                //通用
                baseMerchantAgreementEntity.setName(CommonType.agreement.BR_AGREEMENT.getDesc());
                baseMerchantAgreementEntity.setSign(CommonType.agreement.BR_AGREEMENT.getType());
            }
            this.baseMapper.addAgreement(baseMerchantAgreementEntity);
        } else {
            //存入新url
            baseMerchantAgreementEntity.setUrl(url);
            //修改
            this.baseMapper.addAgreement(baseMerchantAgreementEntity);
        }
        return ResponseResult.success(url);
    }

    @Override
    public ResponseResult queryAgreement(Map<String, Object> map) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(map.get("token").toString());
        if (null != userInfo) {
            List<BaseMerchantAgreementEntity> baseMerchantAgreementEntities = this.baseMapper.selectList(
                    new LambdaQueryWrapper<BaseMerchantAgreementEntity>()
                            .eq(map.get("sign") != null, BaseMerchantAgreementEntity::getSign, map.get("sign")));
            return ResponseResult.success(baseMerchantAgreementEntities);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

}
